package com.yzg.executors;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;

/**
 * @author misterWei
 * @create 2020年06月13号:02点35分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.SynchronousQueueMain")
public class SynchronousQueueMain {
    public static void main(String[] args) {
        SynchronousQueue<String> strings = new SynchronousQueue<>();
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), (t) -> new Thread(t, String.valueOf(System.currentTimeMillis())));
        executorService.execute(()->{
            try {
                //没有其它线程拿,就会一直进行阻塞
                strings.put("1");
                log.info("拿到了...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        executorService.execute(()->{
            try {
                //没有其它线程拿,就会一直进行阻塞
                strings.put("2");
                log.info("拿到了...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        executorService.execute(()->{
            try {
                //没有其它线程拿,就会一直进行阻塞
                System.out.println(strings.take());
                System.out.println(strings.take());
                log.info("取出了...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

    }
}
